package xiao.ke.serviceprovider1.controller;/**
 * @author XuXiaoK
 * @create 2020-01-19 14:25
 */

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import xiao.ke.serviceprovider1.entity.Book;
import xiao.ke.serviceprovider1.entity.Person;


import javax.servlet.http.HttpServletRequest;

/**
 *@Author XiaoKe
 *@Date 2020/1/19 14:25
 *@Description TODO
 */
@RestController
public class DemoController {

    @RequestMapping("/book/{bookId}")
    public Book getBook(@PathVariable("bookId") Integer bookId) {
        Book book = Book.builder().id(bookId).name("你要跳舞吗").Author("xiaoke").build();
        return book;
    }

    /**
     *
     *
     * @param personId
     * @param request
     * @return
     */
    @RequestMapping("/person/{personId}")
    public Person findPerson(@PathVariable("personId") Integer personId,
                             HttpServletRequest request) {
        Person person = new Person(personId, "xiaoke", 22);
        person.setName(request.getRequestURL().toString());
        return person;
    }

    @RequestMapping("/hi")
    public String ello(@RequestParam String name) {
        return "hello " + name;
    }

    @RequestMapping("/hello")
    public String hello(HttpServletRequest request) throws InterruptedException {
        Thread.sleep(800);
        return "hello,"+request.getRequestURL();
    }

    @RequestMapping(value = "/person/create", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public String createPerson(@RequestBody Person person) {
        System.out.println(person.getName() + "--" + person.getAge());

        return "Person:" + person.getId();
    }

    @RequestMapping(value = "/person/createXML", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_XML_VALUE)
    public String createXMLPerson(@RequestBody Person person) {
        System.out.println(person.getName() + "--" + person.getAge());

        return "<result><message>success</meessage></result>";
    }

    /**
     * 测试Hystrix用的服务
     */
    @GetMapping("normalHello")
    public String normalHello(HttpServletRequest request) {
        return "Hello World";
    }

    @GetMapping("errorHello")
    public String errorHello(HttpServletRequest request) throws InterruptedException {
        Thread.sleep(1000);
        return "Error Hello World";
    }


}
